import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import DataBase from 'src/config/database';
import { Repository, Like, UpdateResult } from 'typeorm';
import utils from 'src/common/utils';
import { InternetCelebrityGroupCheck } from './internetCelebrityGroupCheck.entity';

@Injectable()
export class InternetCelebrityGroupCheckService {
  constructor(
    @InjectRepository(InternetCelebrityGroupCheck)
    private repository: Repository<InternetCelebrityGroupCheck>,
    
  ) {}

  async insert(list: InternetCelebrityGroupCheck[]){
    let db: DataBase = new DataBase()
    let connection = await db.getConnection()
    const queryRunner = connection.createQueryRunner()
    await queryRunner.connect()
    // 开始事务
    await queryRunner.startTransaction()
    try {
      let itcList = []
      list.map((e: any) => {
        let item = new InternetCelebrityGroupCheck()
        item.platform = e.platform.platform
        item.account = e.ic.account
        item.name = e.ic.name
        item.avatar_img = e.ic.avatar_img
        item.avatar_img_id = e.ic.avatar_img_id
        item.create_time = new Date()
        item.wx_user = e.wx_user
        itcList.push(item)
      })
      await this.repository.createQueryBuilder(null, queryRunner)
        .insert()
        .into(InternetCelebrityGroupCheck)
        .values(itcList)
        .execute()
      
      await queryRunner.commitTransaction()
      await queryRunner.release();
    } catch (err) {
      // 有错误做出回滚更改
      await queryRunner.rollbackTransaction()
      await queryRunner.release();
      return err
    }
   
  }

}
